/**
 *  Copyright 2010 ShenYang voole Co. Ltd.
 *  All right reserved. 
 */
package com.wm.dao.hibernate;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wm.bean.Area;
import com.wm.bean.City;
import com.wm.bean.Restaurant;
import com.wm.comm.Constant;
import com.wm.dao.AreaDAO;
import com.wm.dao.CityDAO;
import com.wm.dao.RestaurantDAO;

/**
 * 功能描述:饭店数据操作
 *
 * 创建日期 Dec 29, 2012
 *
 * @author zhangbaochao
 *
 * @version 1.0
 *
 */
public class RestaurantHibernateDAO extends HibernateDaoSupport implements RestaurantDAO{
	
	private static final String LOAD_BY_ID = "from Restaurant where id = ?";
	private static final String REMOVE_BY_ID = "delete Restaurant where id = ?";
	private static final String LOAD_BY_ID_VALID_SHOW = "from Restaurant where id = ? and valid = ? and isShow= ?";
	private static final String LOAD_BY_POINTX_POINTY_VALID_SHOW = "from Restaurant where pointX = ? and pointY=? and valid = ? and isShow= ? order by id desc";
	
	/**
	 * 保存Restaurant对象
	 * 
	 * @param restaurant
	 * 			Restaurant
	 * @return Restaurant
	 * 			Restaurant
	 */
	public Restaurant saveRestaurant(Restaurant restaurant){
		this.getHibernateTemplate().save(restaurant);
		return restaurant;
	}
	
	/**
	 * 更新Restaurant对象
	 * 
	 * @param restaurant
	 * 			Restaurant
	 * @return Restaurant
	 * 			Restaurant
	 */
	public Restaurant updateRestaurant(Restaurant restaurant){
		this.getHibernateTemplate().update(restaurant);
		return restaurant;
	}
	
	/**
	 * 根据ID获取Restaurant对象
	 * 
	 * @param id
	 * 			long
	 */
	public void removeRestaurantById(final long id){
		getHibernateTemplate().execute(new HibernateCallback() {
	        public Object doInHibernate(Session s) throws HibernateException, SQLException {
	          Query query = s.createQuery(REMOVE_BY_ID);
	          query.setLong(0, id);
	          query.executeUpdate();
	          return null;
	        }
	      });
	}
	
	/**
	 * 根据ID获取Restaurant对象
	 * 
	 * @param id
	 * 			long
	 * @return Restaurant
	 * 			Restaurant
	 */
	public Restaurant findRestaurantById(long id){
		List l = this.getHibernateTemplate().find(LOAD_BY_ID,id);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (Restaurant) l.get(0);
		}
	}
	
	/**
	 * 前台查询用
	 * @param id
	 * @param valid
	 * @param isShow
	 * @return
	 */
	public Restaurant findRestaurantByIdValidIsShow(long id,int valid,int isShow){
		Object[] obj={new Long(id),new Integer(valid),new Integer(isShow)};
		List l = this.getHibernateTemplate().find(LOAD_BY_ID_VALID_SHOW,obj);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (Restaurant) l.get(0);
		}
	}
	
	/**
	 * 根据参数获取Restaurant对象列表
	 * 
	 * @param resPinyin
	 * 			String
	 * @param isShow
	 * 			int
	 * @param orderby
	 * @param ascOrDesc
	 * @return List
	 * 			List
	 */
	public List findRestaurantsByResPinyinYinIsShow(String resPinyin,int isShow, String orderby, int ascOrDesc){
		StringBuffer sb = new StringBuffer();
		sb.append("from Restaurant where 1=1");
		if(StringUtils.isNotBlank(resPinyin)){
			sb.append(" and(");
			sb.append(" resAnglic like '%");
			sb.append(resPinyin);
			sb.append("%'");
			sb.append(" or resPhonetic like '%");
			sb.append(resPinyin);
			sb.append("%'");
			sb.append(")");
	    }
		if (isShow != -1) {
			sb.append(" and isShow=");
			sb.append(isShow);
		}
		if (StringUtils.isNotBlank(orderby)) {
			if (ascOrDesc == Constant.ORDER_ASC) {
				sb.append(" order by " + orderby+" asc");
			}
			if (ascOrDesc == Constant.ORDER_DESC) {
				sb.append(" order by " + orderby+" desc");
			}
		}
		final String sql = sb.toString();
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException, SQLException {
				Query query = s.createQuery(sql);
				List list = query.list();
				return list;
			}
		});
	}
	
	/**
	 * 根据参数获取Restaurant对象列表
	 * 
	 * @param isShow
	 * 			int
	 * @param orderby
	 * @param ascOrDesc
	 * @param firstResult
	 * @param maxResults
	 * @return List
	 * 			List
	 */
	public List findRestaurantsByResNameValidIsShow(String resName,int valid,int isShow,String orderby,int ascOrDesc,final int firstResult,final int maxResults){
		StringBuffer sb = new StringBuffer();
		sb.append("from Restaurant where 1=1");
		if(StringUtils.isNotBlank(resName)){
			sb.append(" and resName like '%");
			sb.append(resName);
			sb.append("%'");
		}
		if (valid != -1) {
			sb.append(" and valid=");
			sb.append(valid);
		}
		if (isShow != -1) {
			sb.append(" and isShow=");
			sb.append(isShow);
		}
		if (StringUtils.isNotBlank(orderby)) {
			if (ascOrDesc == Constant.ORDER_ASC) {
				sb.append(" order by " + orderby+" asc");
			}
			if (ascOrDesc == Constant.ORDER_DESC) {
				sb.append(" order by " + orderby+" desc");
			}
		}
		final String sql = sb.toString();
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException, SQLException {
				Query query = s.createQuery(sql);
				query.setFirstResult(firstResult);
				query.setMaxResults(maxResults);
				List list = query.list();
				return list;
			}
		});
	}
	
	/**
	 * 根据参数获取Restaurant对象个数
	 * 
	 * @param isShow
	 * 			int
	 * @return long
	 * 			long
	 */
	public long getRestaurantNumByResNameValidIsShow(String resName,int valid,int isShow){
		StringBuffer sb = new StringBuffer();
		sb.append("select count(id) from Restaurant where 1=1");
		if(StringUtils.isNotBlank(resName)){
			sb.append(" and resName like '%");
			sb.append(resName);
			sb.append("%'");
		}
		if (valid != -1) {
			sb.append(" and valid=");
			sb.append(valid);
		}
		if (isShow != -1) {
			sb.append(" and isShow=");
			sb.append(isShow);
		}
		List l = this.getHibernateTemplate().find(sb.toString());
		
		if (l == null || l.isEmpty()) {
			return 0;
		} else {
			return (Long) l.get(0);
		}
	}
	
	/**
	 * 根据坐标查询
	 * @param pointX
	 * @param pointY
	 * @param valid
	 * @param isShow
	 * @return
	 */
	public List findRestaurantByPointXPointYValidIsShow(double pointX,double pointY,int valid,int isShow ){
		Object[] obj={pointX,pointY,new Integer(valid),new Integer(isShow)};
		return this.getHibernateTemplate().find(LOAD_BY_POINTX_POINTY_VALID_SHOW,obj);
	}
}
